package com.starmark.core.framework.web;


import com.alibaba.fastjson.JSONObject;
import com.starmark.core.framework.base.AbstractController;
import com.starmark.core.framework.response.Response;
import com.starmark.core.framework.response.SuccessResponseData;
import com.starmark.core.framework.service.CommonBeanDataService;
import com.starmark.core.framework.service.CommonBeanPostDataService;
import com.starmark.core.framework.service.CommonIdToNameService;
import com.starmark.core.framework.util.SpringContextUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 通用获取data的相关接口
 *
 * @author starmark
 * <p>
 * 2018年08月13日下午7:21:41
 */
//原来计划全部人实现CommonBeanDataService
// 或CommonIdToNameService就能快速接口化
 //但各服务组件微服化时,没法通用,故写成抽像类,后续使用只需要并指定地址即可
public abstract class SysCommonBeanDataController extends AbstractController {
    /**
     * 通用Controller
     *
     * @param beanId 业务BeanId
     * @param request 请求
     * @return
     */

    @GetMapping(value = "/getData/{beanId}")
    public Response getBeanData(@PathVariable String beanId, HttpServletRequest request) {
        SuccessResponseData successResponseData = new SuccessResponseData();
        CommonBeanDataService beanDataService = (CommonBeanDataService) SpringContextUtils.getBean(beanId);
        successResponseData.setData(beanDataService.getData(request));
        return successResponseData;
    }

    /**
     * 通用Controller
     *
     * @param beanId
     * @param request
     * @return
     */

    @GetMapping(value = "/getNameById/{beanId}")
    public Response getNameById(@PathVariable String beanId, HttpServletRequest request) {
        SuccessResponseData successResponseData = new SuccessResponseData();
        CommonIdToNameService beanDataService = (CommonIdToNameService) SpringContextUtils.getBean(beanId);
        String id = request.getParameter("id");
        Assert.hasText(id, "The id is not allow empty!");
        successResponseData.setData(beanDataService.getNameById(id));
        return successResponseData;
    }


    /**
     * 通用Controller
     *
     * @param beanId 业务BeanId
     * @param jsonObject 请求数 据
     * @return 获取Post请求数据
     */

    @PostMapping(value = "/getPostData/{beanId}")
    public Response getPostData(@PathVariable String beanId, @RequestBody JSONObject jsonObject) {
        SuccessResponseData successResponseData = new SuccessResponseData();
        CommonBeanPostDataService beanDataService = (CommonBeanPostDataService) SpringContextUtils.getBean(beanId);
        successResponseData.setData(beanDataService.getData(jsonObject));
        return successResponseData;
    }

}
